package mobileann.mafamily.utils;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

import mobileann.mafamily.act.main.FS;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Debug;

public class MemUtils {
	public static long getAvailMemSize(Context context) {
		ActivityManager.MemoryInfo mmif = new ActivityManager.MemoryInfo();
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		am.getMemoryInfo(mmif);
		return mmif.availMem;
	}

	public static long getTotalMemSize(Context context) {
		long totalMem = 0;
		if (context.getSharedPreferences("totalMem", Context.MODE_PRIVATE)
				.contains("totalMem")) {
			totalMem = context.getSharedPreferences("totalMem",
					Context.MODE_PRIVATE).getLong("totalMem", 0);
		} else {
			FileReader localFileReader;
			try {
				localFileReader = new FileReader("/proc/meminfo");
				BufferedReader localBufferedReader = new BufferedReader(
						localFileReader, 8192);
				String str = localBufferedReader.readLine();
				String[] strs = str.split("\\s+");
				totalMem = Long.valueOf(strs[1]);
				context.getSharedPreferences("totalMem", Context.MODE_PRIVATE)
				.edit().putLong("totalMem", totalMem).commit();
				localFileReader.close();
			} catch (FileNotFoundException e) {
			} catch (IOException e) {
			}
		}
		return totalMem;
	}

	private static int get_mem(String packagename, int pid, Context context) {
		int single_mem = 0;
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		Debug.MemoryInfo[] memoryInfo = am
				.getProcessMemoryInfo(new int[] { pid });
		single_mem = memoryInfo[0].getTotalPss();
		return single_mem;
	}

	public static String getMemUse(Context context) {
		Process pcs = null;
		BufferedReader br = null;
		Runtime rtm = Runtime.getRuntime();
		PackageManager pm = context.getPackageManager();
		String app_Name;
		int app_Pid, app_MemUse;
		String memUse = "";
		try {
			pcs = rtm.exec("ps");
			InputStreamReader isr = new InputStreamReader(pcs.getInputStream());
			br = new BufferedReader(isr);
			String sl = br.readLine();
			while (sl != null) {
				if (sl.startsWith("app") || sl.startsWith("u0")) {
					String[] ss = sl.split("\\s+");
					if (ss.length >= 9) {
						if (ss[8].compareToIgnoreCase("ps") != 0) {
							app_Name = ss[8];
							app_Pid = Integer.valueOf(ss[1]);
							String pkg = FS.getInstance().getPackageName();
							if(pkg.equals(app_Name)){
								app_MemUse = get_mem(app_Name, app_Pid, context);
								memUse = String.format("%.1fM", app_MemUse / 1024.0);
//								L.i("Name", app_Name + "::" + memUse);
							}
						}
					}
				}
				sl = br.readLine();
			}
			br.close();
			pcs.destroy();
		} catch (IOException e) {
			if (br != null)
				try {
					br.close();
				} catch (IOException e1) {
				}
			if (pcs != null)
				pcs.destroy();
		}
		return memUse;
	}
}
